package com.ajzhang.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ajzhang.bean.BillType;
import com.ajzhang.bean.UserInfo;
import com.ajzhang.facade.BillTypeFacade;
import com.ajzhang.facade.UserInfoFacade;


public class UserInfoAction extends ActionSupport{
	
	private UserInfo userInfo;
	private UserInfoFacade userInfoFacade;

	@Override
	public String execute() throws Exception {
		System.out.println("into UserInfoAction -> execute()");
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		try {
			String loginName = request.getParameter("loginName");
			String loginPass = request.getParameter("loginPass");
			userInfo = this.userInfoFacade.findUserInfoByuserName(loginName);
			if(userInfo == null){
				out.print("error");
				return null;
			}
			if(userInfo.getUserPass().equals(loginPass)){
				request.getSession().setAttribute("UserInfo", userInfo);
				out.print("success");
				return null;
			}else{
				out.print("error");
				return null;
			}
		} catch (Exception e) {
			System.out.println(e.getMessage()+":登录时出错。");
			out.print("error");
		}
		return null;
	}
	
	public String save() throws Exception {
		System.out.println("into UserInfoAction -> save()");
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		try {
			String userName = request.getParameter("userName");
			String userPass = request.getParameter("userPass");
			String nickName = request.getParameter("nickName");
			String securityCode = request.getParameter("securityCode");
			String address = request.getParameter("address");
			boolean sex = Boolean.parseBoolean(request.getParameter("sex"));
			String birthday = request.getParameter("birthday");
			String headPhoto = request.getParameter("headPhoto");
			userInfo = this.userInfoFacade.findUserInfoByuserName(userName);
			if(userInfo != null){
				out.print("exist");
				return null;
			}
			userInfo = new UserInfo();
			userInfo.setUserName(userName);
			userInfo.setUserPass(userPass);
			userInfo.setNickName(nickName);
			userInfo.setSecurityCode(securityCode);
			userInfo.setAddress(address);
			userInfo.setSex(sex);
			userInfo.setScore(0);
			userInfo.setCreateDate(new Date());
			userInfo.setHeadPhoto(headPhoto);
			userInfo.setBirthday(birthday);
			this.userInfoFacade.save(userInfo);
			out.print("success");
		} catch (Exception e) {
			System.out.println(e.getMessage()+":保存UserInfo时出错。");
			out.print("error");
		}
		
		return null;
	}
	
	public String update() throws Exception {
		return null; 
	}
	
	public String remove() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			request.getSession().removeAttribute("UserInfo");
		} catch (Exception e) {
			return "login";
		}
		return "login";
	}
	
	public String validationUserName() throws Exception {
		System.out.println("into UserInfoAction -> validationUserName()");
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		try {
			String userName = request.getParameter("userName");
			userInfo = this.userInfoFacade.findUserInfoByuserName(userName);
			if(userInfo == null){
				out.print("success");
			}else{
				out.print("not");
			}
		} catch (Exception e) {
			System.out.println(e.getMessage()+":验证帐号是否已存在时出错。");
			out.print("error");
		}
		return null;
	}
	public UserInfo getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	public UserInfoFacade getUserInfoFacade() {
		return userInfoFacade;
	}
	public void setUserInfoFacade(UserInfoFacade userInfoFacade) {
		this.userInfoFacade = userInfoFacade;
	}

}
